import { Tools } from "../../common/Tools";
import { EventManager } from "../../manager/EventManager";
import LvItem from "../../LvItem";
import LvItem_15 from "./LvItem_15";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../../base/EnumIndex";
import LevelPre_15 from "./LevelPre_15";
import { UIManager } from "../../manager/UIManager";
import { AudioManager1 } from "../../manager/AudioManager1";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_15_174 extends cc.Component {

    level15: LevelPre_15 = null

    diwei: number = 0;
    diweiArray = ["奴婢", "官女子", "答应", "贵人", "妃子", "贵妃", "皇贵妃", "皇后"];

    /** 皇帝出现 */
    huangdichuxian = false

    lastNode = null

    start(): void {
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this)
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);


        this.diweibianhua();

    }
    setAttachment(node: cc.Node, slotName: string, attachmentName: string) {
        let spine = node.getComponent(sp.Skeleton);
        spine.setAttachment(slotName, attachmentName);
    };

    setBoneActive(node: cc.Node, boneName: string, isActive: boolean) {

        let bone = node.getComponent(sp.Skeleton).findBone(boneName);
        if (!bone) { return; }
        bone.active = isActive;

    };


    finishTween(finishStr: string, lvItem: LvItem) {
        if (typeof finishStr != "string") {
            return;
        }

        if (finishStr.includes("reviveGame")) {
            AudioManager1.playBG("古风旋律",0.3,BUNDLE_TYPE_ENUM.LEVEL)
            this.diweibianhua();
            this.gN("huangdi").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);
            if (this.huangdichuxian == false) {
                this.gN("huangdi").setPosition(645, -268);
            }
            this.lastNode.active = true;
            this.gN("失败场景").active = false;


            this.gN("xiandainv").getComponent(sp.Skeleton).setAttachment("待机胳膊", "待机胳膊");
            this.gN("xiandainv").getComponent(sp.Skeleton).findBone("花瓶").active = false;
            UIManager.ins.enabledGameClick(true);
        }
        if (finishStr.includes("升级")) {
            this.diwei++;
            this.diweibianhua();
        }

        if (finishStr.includes("皇帝出现")) {
            this.huangdichuxian = true;
            this.level15.addHaveStates(["皇帝出现"])
        }

        if (finishStr.includes("lastNode")) {
            this.lastNode = lvItem.node;
        }
    }

    diweibianhua() {
        let diweiNode = this.gN("地位").children
        for (const key in diweiNode) {
            const element = diweiNode[key];
            if (element.name == this.diweiArray[this.diwei]) {
                element.active = true;
            } else {
                element.active = false;
            }
        }
        if (this.diwei < 4) {
            this.setAttachment(this.gN("gudainv"), "大白脸", "奴婢、官女子、答应、贵人脸");
        } else if (this.diwei < 6) {
            this.setAttachment(this.gN("gudainv"), "大白脸", "妃子、贵妃脸");
        } else {
            this.setAttachment(this.gN("gudainv"), "大白脸", "皇贵妃皇后脸");
        }

        this.setAttachment(this.gN("gudainv"), "皇后", this.diweiArray[this.diwei]);
        this.setAttachment(this.gN("gudainv"), "皇后帽子", this.diweiArray[this.diwei] + "发型");
    }

    gN(NodeName: string) {
        return this.level15.getNodeForName(NodeName);
    }

    /** 获取世界Scale */
    getWorldScale(tempScale: number = 1, node: cc.Node) {
        if (node.parent) {
            return this.getWorldScale(tempScale / node.parent.scale, node.parent);
        }
        return tempScale;
    };


    onDisable() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
    }
}
